//
//  TestArrayVC.m
//  技术点总结
//
//  Created by tianlong on 2018/6/7.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "TestArrayVC.h"
#import "Person.h"

@interface TestArrayVC ()

@end

@implementation TestArrayVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

// 不可变数组
- (IBAction)testArrayI:(id)sender {
    
    Person *p = [[Person alloc] init];
    p.name = @"testArrayI0";
    p.dog.name = @"旺财";
    p.dog.food = [[Food alloc] init];
    p.dog.food.name = @"狗粮";
    
    Person *p1 = [[Person alloc] init];
    p1.name = @"testArrayI1";
    p1.dog.name = @"二哈";
    
    NSArray *orig = [NSArray arrayWithObjects:p,p1, nil];
    NSArray *arr1 = orig;
    NSArray *arr2 = [orig copy];
    NSArray *arr3 = [orig mutableCopy];
    NSLog(@"\norig = %p     p      = %p\narr1 = %p     arr1_p = %p    arr1_p.name = %@    arr1_class = %@\narr2 = %p     arr2_p = %p    arr2_p.name = %@    arr2_class = %@\narr3 = %p     arr3_p = %p    arr3_p.name = %@    arr3_class = %@",orig,p,arr1,arr1[0],[arr1[0] name],NSStringFromClass([arr1 class]),arr2,arr2[0],[arr2[0] name],NSStringFromClass([arr2 class]),arr3,arr3[0],[arr3[0] name],NSStringFromClass([arr3 class]));
    
    if ([arr1 isKindOfClass:[NSMutableArray class]]) {
        [(NSMutableArray *)arr1 addObject:@"arr1"];
        NSLog(@"arr1 = %@",arr1);
    }
    if ([arr2 isKindOfClass:[NSMutableArray class]]) {
        [(NSMutableArray *)arr2 addObject:@"arr2"];
        NSLog(@"arr2 = %@",arr2);
    }
    if ([arr3 isKindOfClass:[NSMutableArray class]]) {
        [(NSMutableArray *)arr3 addObject:@"arr3"];
        NSLog(@"arr3 = %@",arr3);
    }
    /**
     temp = 0x60000022f980     p      = 0x60000001e660
     arr1 = 0x60000022f980     arr1_p = 0x60000001e660    arr1_p.name = testArrayI0    arr1_class = __NSArrayI
     arr2 = 0x60000022f980     arr2_p = 0x60000001e660    arr2_p.name = testArrayI0    arr2_class = __NSArrayI
     arr3 = 0x600000440a80     arr3_p = 0x60000001e660    arr3_p.name = testArrayI0    arr3_class = __NSArrayM
     结论：
     对不可变数组进行操作时
     等号和copy：对数组浅拷贝，对元素浅拷贝，真实类型：不可变
     mutableCopy：对数组深拷贝，对元素浅拷贝，真实类型：可变
     */
}

// 可变数组
- (IBAction)testArrayM:(id)sender {
    Person *p = [[Person alloc] init];
    p.name = @"testArrayM0";
    p.dog.name = @"旺财";
    
    Person *p1 = [[Person alloc] init];
    p1.name = @"testArrayM1";
    p1.dog.name = @"二哈";
    
    NSMutableArray *orig = [NSMutableArray arrayWithObjects:p,p1, nil];
    NSArray *arr1 = orig;
    NSArray *arr2 = [orig copy];
    NSArray *arr3 = [orig mutableCopy];
    NSLog(@"\norig = %p     p      = %p\narr1 = %p     arr1_p = %p    arr1_p.name = %@    arr1_class = %@\narr2 = %p     arr2_p = %p    arr2_p.name = %@    arr2_class = %@\narr3 = %p     arr3_p = %p    arr3_p.name = %@    arr3_class = %@",orig,p,arr1,arr1[0],[arr1[0] name],NSStringFromClass([arr1 class]),arr2,arr2[0],[arr2[0] name],NSStringFromClass([arr2 class]),arr3,arr3[0],[arr3[0] name],NSStringFromClass([arr3 class]));
    
    if ([arr1 isKindOfClass:[NSMutableArray class]]) {
        [(NSMutableArray *)arr1 addObject:@"arr1"];
        NSLog(@"arr1 = %@",arr1);
    }
    if ([arr2 isKindOfClass:[NSMutableArray class]]) {
        [(NSMutableArray *)arr2 addObject:@"arr2"];
        NSLog(@"arr2 = %@",arr2);
    }
    if ([arr3 isKindOfClass:[NSMutableArray class]]) {
        [(NSMutableArray *)arr3 addObject:@"arr3"];
        NSLog(@"arr3 = %@",arr3);     }
    
    /**
     orig = 0x60400003e340     p      = 0x6040000dcfc0
     arr1 = 0x60400003e340     arr1_p = 0x6040000dcfc0    arr1_p.name = testArrayI0    arr1_class = __NSArrayI
     arr2 = 0x60400003e340     arr2_p = 0x6040000dcfc0    arr2_p.name = testArrayI0    arr2_class = __NSArrayI
     arr3 = 0x6040002562c0     arr3_p = 0x6040000dcfc0    arr3_p.name = testArrayI0    arr3_class = __NSArrayM
     对不可变数组进行操作
     等号赋值：对数组浅拷贝，对元素浅拷贝，真实类型：可变
     copy： 对数组深拷贝，对元素浅拷贝，真实类型：不可变
     mutableCopy：对数组深拷贝，对元素浅拷贝，真实类型：可变数组
     */
}

@end
